// pages/video/video.js
import request from '../../utils/request.js'
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		videoGroupList: [], //导航标签数据
		navId: '', //当前点击的导航id
		videoList: [], //视频列表
		videoId: '', //当前点击的视频
		videoUpdateTime: [], //存储播放视频的进度
		isTriggered: false, //标识下拉刷新是否触发
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		this.getVideoGroupList()

	},

	//获取导航数据
	async getVideoGroupList() {
		let res = await request('/video/group/list')
		this.setData({
			videoGroupList: res.data.slice(0, 14),
			navId: res.data[0].id
		})
		this.getVideoList(this.data.navId)
	},
	//获取video列表数据
	async getVideoList(navId) {
		wx.showLoading({
			title: '加载中',
		})
		this.setData({
			videoList: []
		})
		let res = await request('/video/group', {
			id: navId
		})
		let index = 0
		res.datas.map(item => {
			item.id = index++
			return item
		})
		this.setData({
			videoList: res.datas,
			isTriggered: false
		})
		wx.hideLoading()
		//关闭下拉刷新
		
	},

	//点击切换导航
	changeNav(e) {
		this.setData({
			// navId: Number(e.currentTarget.id)
			// 位移运算符，转成number
			navId: e.currentTarget.id >>> 0
		})
		this.getVideoList(this.data.navId)
	},

	//点击播放、继续播放回调
	handlePlay(e) {
		let vid = e.currentTarget.id
		//创建控制video标签的实例对象
		this.vid !== vid && this.videoContext && this.videoContext.stop()
		this.vid = vid
		this.setData({
			videoId: vid
		})
		this.videoContext = wx.createVideoContext(vid)
		//判断当前视频是否播放过
		let {
			videoUpdateTime
		} = this.data
		let videoItem = videoUpdateTime.find(item => item.vid === vid)
		//如果有播放记录则跳转到上次播放的位置
		if (videoItem) {
			this.videoContext.seek(videoItem.currentTime)
		}
	},
	//监听视频播放进度
	handleTimeUpdate(e) {
		let videoTimeObj = {
			vid: e.currentTarget.id,
			currentTime: e.detail.currentTime
		}
		let {
			videoUpdateTime
		} = this.data
		let videoItem = videoUpdateTime.find(item => item.vid === videoTimeObj.vid)
		if (videoItem) {
			videoItem.currentTime = e.detail.currentTime
		} else {
			videoUpdateTime.push(videoTimeObj)
			this.setData({
				videoUpdateTime
			})
		}
	},
	//视频播放结束的回调
	handleEnded(e) {
		let {videoUpdateTime} = this.data
		videoUpdateTime.splice(videoUpdateTime.findIndex(item => item.vid === e.currentTarget.id),1)
		this.setData({
			videoUpdateTime
		})
	},
	//针对scroll的自定义下拉刷新
	handleRefresher() {
		this.getVideoList(this.data.navId)
	},
	
	toSearch() {
		wx.navigateTo({
			url: '/pages/search/search'
		})
	},


	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function({from}) {
		if (from === 'button') {
			return {
				title: 'button转发',
				page: '/pages/video/video',
				imageUrl: '/static/images/nvsheng.jpg'
			}
		}else {
			return {
				title: 'menu转发',
				page: '/pages/video/video',
				imageUrl: '/static/images/nvsheng.jpg'
			}
		}
	}
})
